home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / oort / text.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.3 KB  |  367 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*------------------------------------------------------------------------------
  18.  *
  19.  * OORT - text.c - Routines for displaying and handling text.
  20.  *
  21.  * $Id: text.c,v 1.3 1994/01/28 00:21:46 mtj Exp $
  22.  * 
  23.  * Chris Fouts - May, 1993.
  24.  * 
  25.  *----------------------------------------------------------------------------*/
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <unistd.h>
  30. #include <bstring.h>
  31. #include <fcntl.h>
  32. #include <gl.h>
  33. #include <Performer/pf.h>
  34.  
  35. #include "text.h"
  36. #include "oort.h"
  37. #include "load.h"
  38.  
  39. /* BEGIN PROTOTYPES -S text.c */
  40. static void     drawLines( short *data ) ;
  41. static void     drawMesh( short *data ) ;
  42. /* END PROTOTYPES -S text.c */
  43.  
  44.  
  45.  
  46. static float    xPos = 0.0f ;
  47. static float    yPos = 0.0f ;
  48.  
  49.  
  50.  
  51. /*------------------------------------------------------------------------------
  52.  * Load a mesh or outline font.
  53.  *----------------------------------------------------------------------------*/
  54. GeoFont *
  55. loadFont(
  56.     char    *name
  57.     )
  58. {
  59.     int        fd ;
  60.     long        nChars ;
  61.     long        magic ;
  62.     long        datalen ;
  63.     char        *fileName ;
  64.     GeoFont        *gf ;
  65.  
  66.     gf = (GeoFont *)myMalloc( sizeof( GeoFont ) ) ;
  67.  
  68.     fileName = findFile( name ) ;
  69.  
  70.     if( gf == NULL )
  71.     {
  72.         return( NULL ) ;
  73.     }
  74.  
  75.     if( ( fd = open( fileName, O_RDONLY ) ) < 0 )
  76.     {
  77.         perror( fileName ) ;
  78.         exit( 1 ) ;
  79.     }
  80.  
  81.     readIn( fd, &magic, sizeof( magic ) ) ;
  82.  
  83.     if( magic != OORT_FONT )
  84.     {
  85.         fprintf( stderr, "%s: bad magic number\n", fileName ) ;
  86.         exit( 1 ) ;
  87.     }
  88.  
  89.     readIn( fd, &(gf->type), sizeof( gf->type ) ) ;
  90.     readIn( fd, &(gf->minChar), sizeof( gf->minChar ) ) ;
  91.     readIn( fd, &(gf->maxChar), sizeof( gf->maxChar ) ) ;
  92.     readIn( fd, &(gf->heightUp), sizeof( gf->heightUp ) ) ;
  93.     readIn( fd, &(gf->heightLo), sizeof( gf->heightLo ) ) ;
  94.     nChars = gf->maxChar - gf->minChar + 1 ;
  95.  
  96.     gf->widths = (short *)myMalloc( nChars * sizeof( short ) ) ;
  97.     readIn( fd, gf->widths, nChars * sizeof( short ) ) ;
  98.  
  99.     gf->offsets = (short *)myMalloc( nChars * sizeof( short ) ) ;
  100.     readIn( fd, gf->offsets, nChars * sizeof( short ) ) ;
  101.  
  102.     readIn( fd, &datalen, sizeof( datalen ) ) ;
  103.  
  104.     gf->data = (short *)myMalloc( datalen * sizeof( short ) ) ;
  105.     readIn( fd, gf->data, datalen * sizeof( short ) ) ;
  106.  
  107.     close( fd ) ;
  108.  
  109.  
  110.     if( gf->type == 1 )
  111.     {
  112.         gf->draw = drawMesh ;
  113.     }
  114.     else
  115.     {
  116.         gf->draw = drawLines ;
  117.     }
  118.  
  119.     return( gf ) ;
  120. }
  121.  
  122.  
  123.  
  124. /*------------------------------------------------------------------------------
  125.  * Draw a font string.
  126.  *----------------------------------------------------------------------------*/
  127. void
  128. drawString(
  129.     GeoFont        *gf,
  130.     char        *str
  131.     )
  132. {
  133.     char    c ;
  134.     int    i = 0 ;
  135.  
  136.     pfPushMatrix() ;
  137.     translate( xPos, yPos, 0.0f ) ;
  138.     scale( gf->scale, gf->scale, 1.0f ) ;
  139.     translate( 0.0f, (float)-gf->heightLo, 0.0f ) ;
  140.     while( ( c = str[i] ) )
  141.     {
  142.         if( gf->minChar <= c && c <= gf->maxChar )
  143.         {
  144.             c -= gf->minChar ;
  145.             if( gf->offsets[c] >= 0 )
  146.             {
  147.                 gf->draw( gf->data + gf->offsets[c] ) ;
  148.             }
  149.             translate( gf->widths[c], 0.0f, 0.0f ) ;
  150.         }
  151.         i++ ;
  152.     }
  153.     pfPopMatrix() ;
  154. }
  155.  
  156.  
  157.  
  158. /*------------------------------------------------------------------------------
  159.  * Draw a tmeshed font string.
  160.  *----------------------------------------------------------------------------*/
  161. static void
  162. drawMesh(
  163.     short    *data
  164.     )
  165. {
  166.     int    i = 0 ;
  167.     int    npts ;
  168.     short    glyph ;
  169.  
  170.     if( *data != BGNTMESH )
  171.     {
  172.         fprintf( stderr, "drawMesh: bad start info (%hd)\n", *data ) ;
  173.         return ;
  174.     }
  175.  
  176.     while( ( glyph = data[i++] ) != RETENDTMESH )
  177.     {
  178.         switch( glyph )
  179.         {
  180.             case BGNTMESH :
  181.                 bgntmesh() ;
  182.                 break ;
  183.             case SWAPTMESH :
  184.                 swaptmesh() ;
  185.                 break ;
  186.             case ENDBGNTMESH :
  187.                 endtmesh() ;
  188.                 bgntmesh() ;
  189.                 break ;
  190.             default :
  191.                 fprintf( stderr, "drawMesh: bad info (%hd)\n",
  192.                     glyph ) ;
  193.                 return ;
  194.         }
  195.  
  196.         npts = data[i++] ;
  197.         while( npts-- > 0 )
  198.         {
  199.             v2s( data + i ) ;
  200.             i += 2 ;
  201.         }
  202.     }
  203.     endtmesh() ;
  204. }
  205.  
  206.  
  207.  
  208. /*------------------------------------------------------------------------------
  209.  * Draw an outline font string.
  210.  *----------------------------------------------------------------------------*/
  211. static void
  212. drawLines(
  213.     short    *data
  214.     )
  215. {
  216.     int    i = 0 ;
  217.     int    npts ;
  218.     short    glyph ;
  219.  
  220.     while( ( glyph = data[i++] ) != RETENDLOOP )
  221.     {
  222.         switch( glyph )
  223.         {
  224.             case BGNLOOP :
  225.                 bgnclosedline() ;
  226.                 break ;
  227.             case ENDBGNLOOP :
  228.                 endclosedline() ;
  229.                 bgnclosedline() ;
  230.                 break ;
  231.             case RETLOOP :
  232.                 endclosedline() ;
  233.                  return ;
  234.             default :
  235.                 fprintf( stderr, "drawMesh: bad info (%hd)\n",
  236.                     glyph ) ;
  237.                 return ;
  238.         }
  239.  
  240.         npts = data[i++] ;
  241.         while( npts-- > 0 )
  242.         {
  243.             v2s( data + i ) ;
  244.             i += 2 ;
  245.         }
  246.     }
  247.     endclosedline() ;
  248. }
  249.  
  250.  
  251.  
  252. /*------------------------------------------------------------------------------
  253.  * Set the scale factor of the font.
  254.  *----------------------------------------------------------------------------*/
  255. void
  256. setFontSize(
  257.     GeoFont        *gf,
  258.     float        size
  259.     )
  260. {
  261.     gf->scale = size / (float)( gf->heightUp - gf->heightLo ) ;
  262. }
  263.  
  264.  
  265.  
  266. /*------------------------------------------------------------------------------
  267.  * Get the font height in real world coordinates.
  268.  *----------------------------------------------------------------------------*/
  269. float
  270. getFontSize(
  271.     GeoFont        *gf
  272.     )
  273. {
  274.     return( gf->scale * (float)( gf->heightUp - gf->heightLo ) ) ;
  275. }
  276.  
  277.  
  278.  
  279. /*------------------------------------------------------------------------------
  280.  * Return the number of characters that will fit in a given width.
  281.  *----------------------------------------------------------------------------*/
  282. int
  283. maxCharsInWidth(
  284.     GeoFont        *gf,
  285.     char        *str,
  286.     float        width
  287.     )
  288. {
  289.     int    i = 0 ;
  290.     float    w = 0.0f ;
  291.  
  292.     while( str[i] )
  293.     {
  294.         if( gf->minChar <= str[i] && str[i] <= gf->maxChar )
  295.         {
  296.             w += gf->scale * gf->widths[str[i] - gf->minChar] ;
  297.             if( w > width )
  298.             {
  299.                 break ;
  300.             }
  301.         }
  302.         i++ ;
  303.     }
  304.  
  305.     return( i ) ;
  306. }
  307.  
  308.  
  309.  
  310. /*------------------------------------------------------------------------------
  311.  * Return the width of a string.
  312.  *----------------------------------------------------------------------------*/
  313. float
  314. getStrWidth(
  315.     GeoFont    *gf,
  316.     char    *str
  317.     )
  318. {
  319.     int    i = 0 ;
  320.     float    w = 0.0f ;
  321.  
  322.     while( str[i] )
  323.     {
  324.         if( gf->minChar <= str[i] && str[i] <= gf->maxChar )
  325.         {
  326.             w += gf->widths[str[i] - gf->minChar] ;
  327.         }
  328.         i++ ;
  329.     }
  330.  
  331.     return( w * gf->scale ) ;
  332. }
  333.  
  334.  
  335.  
  336. /*------------------------------------------------------------------------------
  337.  * Move the text cursor.
  338.  *----------------------------------------------------------------------------*/
  339. void
  340. positionText(
  341.     float    x,
  342.     float    y
  343.     )
  344. {
  345.     xPos = x ;
  346.     yPos = y ;
  347. }
  348.  
  349.  
  350.  
  351. /*------------------------------------------------------------------------------
  352.  * Copy a font structure.
  353.  *----------------------------------------------------------------------------*/
  354. GeoFont *
  355. copyFont(
  356.     GeoFont    *src
  357.     )
  358. {
  359.     GeoFont    *dst ;
  360.  
  361.     dst = (GeoFont *)myMalloc( sizeof( GeoFont ) ) ;
  362.  
  363.     bcopy( src, dst, sizeof( GeoFont ) ) ;
  364.  
  365.     return( dst ) ;
  366. }
  367.